<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class SettleController extends Controller {
	public function index(){
		//结算
		//开奖结果
		//$current_number = M('number')-> where("periodnumber = 639732")->order('id DESC')->find();
		$current_number = M('number')->order('id DESC')->find();
		
		$number1 = explode(',', $current_number['awardnumbers']);
		
		$lh = unserialize($current_number['lh']);
		for($i=0;$i<count($number1);$i++){
					if($number1[$i] % 2 == 0){
						$number[$i]['ds'] = '双';
						if($number1[$i]>=6){
							$number[$i]['zuhe'] = '大双';
						}else{
							$number[$i]['zuhe'] = '小双';
						}
					}else{
						$number[$i]['ds'] = '单';
						if($number1[$i]>=6){
							$number[$i]['zuhe'] = '大单';
						}else{
							$number[$i]['zuhe'] = '小单';
						}
					}
					if($number1[$i]>=6){
						$number[$i]['dx'] = '大';
					}else{
						$number[$i]['dx'] = '小';
					}
				}
				//当前局所有竞猜
				$list = M('order')->where("number = {$current_number['periodnumber']} && state = 1 && is_add = 0")->order("time ASC")->select();
				// var_dump($list);
				// exit();
				if($list){
					for($i=0;$i<count($list);$i++){
						$id = $list[$i]['id'];
						$userid = $list[$i]['userid'];
						
						//分类
						switch($list[$i]['type']){
							//车号大小单双(12345/双/100)
							case 1:
								$start1 = explode('/', $list[$i]['jincai']);
								$num1 = 0;
								$starts1 = str_split($start1[0]);
								if($start1[1]=='单' || $start1[1]=='双'){
									for($a=0;$a<count($starts1);$a++){
										if($starts1[$a]==0){
											$hao1 = '9';
										}else{
											$hao1 = $starts1[$a]-1;
										}
										if($number[$hao1]['ds']==$start1[1]){
											$num1++;
										}
									}
								}else{
									for($a=0;$a<count($starts1);$a++){
										if($starts1[$a]==0){
											$hao1 = '9';
										}else{
											$hao1 = $starts1[$a]-1;
										}
										if($number[$hao1]['dx']==$start1[1]){
											$num1++;
										}
									}
								}
								if($num1>0){
									$points1 = $num1*$start1[2]*C('dxds');
									$res1 = $this->add_points($id,$userid,$points1);
									if($res1){
										//$this->send_msg('pointsadd',$points1,$userid);
									}
								} 
								break;
								
							//车号(12345/89/20)
							case 2:
								$start2 = explode('/', $list[$i]['jincai']);
								$chehao2 = str_split($start2[1]);
								$starts2 = str_split($start2[0]);
								$num2 = 0;
								for($s=0;$s<count($chehao2);$s++){
									for($a=0;$a<count($starts2);$a++){
										if($starts2[$a]==0){
											$hao2 = '9';
										}else{
											$hao2 = $starts2[$a]-1;
										}
										if($chehao2[$s]==0){
											$chehao2[$s]=10;
										}
										if($chehao2[$s]==$number1[$hao2]){
											$num2++;
										}
									}
								}
								if($num2>0){
									$points2 = $num2*$start2[2]*C('chehao');
									$res2 = $this->add_points($id,$userid,$points2);
									if($res2){
										//$this->send_msg('pointsadd',$points2,$userid);
									}
								} 
								break;
								
							//组合(890/大单/50)
							case 3:
								$start3 = explode('/', $list[$i]['jincai']);
								$starts3 = str_split($start3[0]);
								$num3 = 0;
								for($a=0;$a<count($starts3);$a++){
									if($starts3[$a]==0){
										$hao3 = '9';
									}else{
										$hao3 = $starts3[$a]-1;
									}
									if($number[$hao3]['zuhe']==$start3[1]){
										$num3++;
									}
								}
								if($num3>0){
									if($start3[1]=='大单' || $start3[1]=='小双'){
										$points3 = $num3*$start3[2]*C('zuhe_1');
									}else{
										$points3 = $num3*$start3[2]*C('zuhe_2');
									}
									$res3 = $this->add_points($id,$userid,$points3);
									if($res3){
										//$this->send_msg('pointsadd',$points3,$userid);
									}
								} 
								break;
								
							//龙虎(123/龙/100)
							case 4:
								$start4 = explode('/', $list[$i]['jincai']);
								$starts4 = str_split($start4[0]);
								$num4 = 0;
								for($a=0;$a<count($starts4);$a++){
									if($starts4[$a]==0){
										$hao4 = '9';
									}else{
										$hao4 = $starts4[$a]-1;
									}
									if($lh[$hao4]==$start4[1]){
										$num4++;
									}
								}
								if($num4>0){
									$points4 = $num4*$start4[2]*C('lh');
									$res4 = $this->add_points($id,$userid,$points4);
									if($res4){
										//$this->send_msg('pointsadd',$points4,$userid);
									}
								} 
								break;
								
							//冠亚庄闲(庄/200)
							case 5:
								$start5 = explode('/', $list[$i]['jincai']);
								if($current_number['zx'] == $start5[0]){
									$points5 = $start5[1]*C('zx');
									$res5 = $this->add_points($id,$userid,$points5);
									if($res5){
										//$this->send_msg('pointsadd',$points5,$userid);
									}
								}
								break;
								
							//冠亚号码(组/1-9.3-7/100)
							case 6:
								$start6 = explode('/', $list[$i]['jincai']);
								if(strlen($start6[1])>3){
									$zu = explode('.', $start6[1]);
									for($a=0;$a<count($zu);$a++){
										$gy = explode('-', $zu[$a]);
										if($gy[0]==0){
											$gy[0]=10;
										}
										if($gy[1]==0){
											$gy[1]=10;
										}
										if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
											$num6 = 1;
										}
									}
								}else{
									$gy = explode('-', $start6[1]);
									if($gy[0]==0){
										$gy[0]=10;
									}
									if($gy[1]==0){
										$gy[1]=10;
									}
									if($gy[0]==$number1[0] && $gy[1]==$number1[1] || $gy[0]==$number1[1] && $gy[1]==$number1[0]){
										$num6 = 1;
									}
								}
								if($num6>0){
									$points6 = $num6*$start6[2]*C('gy');
									$res6 = $this->add_points($id,$userid,$points6);
									if($res6){
										//$this->send_msg('pointsadd',$points6,$userid);
									}
								} 
								break;
								
							//特码大小单双(和双100)
							case 7:
								$start7 = substr($list[$i]['jincai'], 3,3); 
								$starts7 = substr($list[$i]['jincai'], 6);
								$num7 = 0;
								if($start7=='大' || $start7=='小'){
									if($current_number['tema_dx']==$start7){
										$num7 = 1;
									}
								}else{
									if($current_number['tema_ds']==$start7){
										$num7 = 1;
									}
								}
								if($num7>0){
									if($start7=='大' || $start7=='双'){
										$points7 = $starts7*C('tema_1');
									}else{
										$points7 = $starts7*C('tema_2');
									}
									$res7 = $this->add_points($id,$userid,$points7);
									if($res7){
										//$this->send_msg('pointsadd',$points7,$userid);
									}
								}
								break;
								
							//特码数字(和5.6.7/100)
							case 8:
								$tema1 = array('03','04','18','19');
								$tema2 = array('5','6','16','17');
								$tema3 = array('7','8','14','15');
								$tema4 = array('9','10','12','13');
								$tema5 = array('11');
								
								$start8 = explode('/', $list[$i]['jincai']);
								$starts8 = substr($start8[0], 3);
								$num8 = 0;
								if(strlen($starts8)>1){
									$tlist = explode('.', $starts8);
									for($a=0;$a<count($tlist);$a++){
										if($current_number['tema']==$tlist[$a]){
											if(in_array($tlist[$a], $tema1)){
												$points8 = $start8[1]*C('tema_sz_1');
											}
											if(in_array($tlist[$a], $tema2)){
												$points8 = $start8[1]*C('tema_sz_2');
											}
											if(in_array($tlist[$a], $tema3)){
												$points8 = $start8[1]*C('tema_sz_3');
											}
											if(in_array($tlist[$a], $tema4)){
												$points8 = $start8[1]*C('tema_sz_4');
											}
											if(in_array($tlist[$a], $tema5)){
												$points8 = $start8[1]*C('tema_sz_5');
											}
											$num8 = 1;
										}
									}
								}else{
									if($current_number['tema']==$starts8){
										if(in_array($starts8, $tema1)){
											$points8 = $start8[1]*C('tema_sz_1');
										}
										if(in_array($starts8, $tema2)){
											$points8 = $start8[1]*C('tema_sz_2');
										}
										if(in_array($starts8, $tema3)){
											$points8 = $start8[1]*C('tema_sz_3');
										}
										if(in_array($starts8, $tema4)){
											$points8 = $start8[1]*C('tema_sz_4');
										}
										if(in_array($starts8, $tema5)){
											$points8 = $start8[1]*C('tema_sz_5');
										}
										$num8 = 1;
									}
								}
								if($num8>0){
									$res8 = $this->add_points($id,$userid,$points8);
									if($res8){
										//$this->send_msg('pointsadd',$points8,$userid);
									}
								} 
								break;
								
							//特码区段(BC/100)
							case 9:
								$start9 = explode('/', $list[$i]['jincai']);
								$num9 = 0;
								if(strlen($start9[0])>1){
									$starts9 = str_split($start9[0]);
									for($a=0;$a<count($starts9);$a++){
										if($current_number['tema_dw']==$starts9[$a]){
											if($starts9[$a]=='A' || $starts9[$a]=='C'){
												$points9 = $start9[1]*C('tema_qd_1');
											}else{
												$points9 = $start9[1]*C('tema_qd_2');
											}
											$num9 = 1;
										}
									}
								}else{
									if($current_number['tema_dw']==$start9[0]){
										if($start9[0]=='A' || $start9[0]=='C'){
											$points9 = $start9[1]*C('tema_qd_1');
										}else{
											$points9 = $start9[1]*C('tema_qd_2');
										}
										$num9 = 1;
									}
								}
								if($num9>0 && $points9){
									$res9 = $this->add_points($id,$userid,$points9);
									if($res9){
										//$this->send_msg('pointsadd',$points9,$userid);
									}
								}
								break;							
						}
					}
				}	
	}

	/*
	 * 竞猜成功  加分
	 * */
	public function add_points($order_id,$userid,$points){
		$res = M('user')->where("id = $userid")->setInc('points',$points);
		if($res){
			$res1 = M('order')->where("id = $order_id")->setField(array('is_add'=>'1','add_points'=>$points));
		}
		if($res && $res1){
			return 1;
		}
	}
	

}?>